import path from "node:path";
import { Server } from "socket.io";
import { readdir } from "node:fs/promises";
import { getFileNameFromUrl } from "../utils/index.js";

export const registRouterIo = async (httpServer) => {
  const routerModule = path.join(__dirname, "modules");
  const files = await readdir(routerModule);
  files.forEach(async (item) => {
    const routerFilePath = path.join(routerModule, item);
    const fileName = getFileNameFromUrl(routerFilePath);
    const file = (await import(routerFilePath)).default;
    if (fileName === "base") {
      //   app.use('/', file);
    } else {
      //   app.use('/'+fileName, file);
      const io = new Server({
        cors: {
          origin: "*", // 允许任何来源
          methods: ["GET", "POST"], // 允许的 HTTP 请求类型
        },
      });
      io.path("/" + fileName);
      file(io, httpServer)
    }
  });
};
